package com.wwj.server.game;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {

    private static Logger logger = LoggerFactory.getLogger(Main.class);


    public static Client client = null;

    public static void main(String[] args) {

        if (args.length < 4) {
            printParamInfo();
            System.exit(0);
            return;
        }

        try {
            String serverName = args[0];
            Integer serverPort = Integer.parseInt(args[1]);

            String connectHostName = args[2];
            Integer connectPort = Integer.parseInt(args[3]);

            Integer reconnectTime = null;
            Integer rebindTime = null;
            if (args.length != 4) {
                if(args.length < 6) {
                    reconnectTime = Integer.parseInt(args[4]);
                }else {
                    reconnectTime = Integer.parseInt(args[4]);
                    rebindTime = Integer.parseInt(args[5]);
                }

            }
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

            //启动服务器
            Server server = applicationContext.getBean(Server.class);
            server.start(serverName, serverPort);

            //启动客户机 与control进行通信
            client = applicationContext.getBean(Client.class);
            client.connect(connectHostName, connectPort, reconnectTime == null ? 3000 : reconnectTime);
            client.bind(serverName, rebindTime == null ? 5000 : rebindTime);
            client.heartbeat().start();
        } catch (NumberFormatException e) {
            printParamInfo();
            System.exit(0);
        }


    }

    private static void printParamInfo() {
        System.out.println("************************* 传入参数异常 *************************");
        System.out.println("* 参数: ServerName Port Connect-HostName Connect-Port [Connect-ReconnectTime<ms>] [Connect-RebindTime<ms>]");
        System.out.println("**************************************************************");
        System.out.println("* ServerName            : 服务名称(唯一)");
        System.out.println("* Port                  : 服务占用端口号");
        System.out.println("* Connect-HostName      : ControlServer连接地址");
        System.out.println("* Connect-Port          : ControlServer连接端口");
        System.out.println("* Connect-ReconnectTime : 重新连接间隔时间(默认值:3000, 单位：毫秒)");
        System.out.println("* Connect-RebindTime    : 重新绑定间隔时间(默认值:5000, 单位:毫秒)");
        System.out.println("**************************************************************");
    }

}
